package com.ipay.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ipay.dao.CashiersDao;
import com.ipay.dao.QRCodeDao;
import com.ipay.entity.CashiersEntity;
import com.ipay.entity.QRCodeEntity;
import com.ipay.service.QRCodeService;
import com.ipay.utils.Constant;
import com.ipay.utils.DateUtils;
import com.ipay.utils.RandomStringUtils;



@Service("qrCodeService")
public class QRCodeServiceImpl implements QRCodeService {
	@Autowired
	private QRCodeDao qrCodeDao;
	@Autowired
	private CashiersDao cashiersDao;

	@Override
	public List<QRCodeEntity> getListByMerchantId(String merchantId) {
		return qrCodeDao.getListByMerchantId(merchantId);
	}

	@Override
	public QRCodeEntity findCodeDetailsByCodeno(String codeNo) {
		return qrCodeDao.findCodeDetailsByCodeno(codeNo);
	}

	@Override
	public List<QRCodeEntity> findListByUserId(String userId) {
		return qrCodeDao.findListByUserId(Long.valueOf(userId));
	}

	@Override
	public void save(QRCodeEntity qrCodeEntity) {
		
		String date = DateUtils.format(new Date(), "yyyyMMdd");
        String random = RandomStringUtils.randomNumeric(6);
        String codeNo = date + random;
		
		StringBuffer qrurl = new StringBuffer();
		//下载时访问地址
		qrurl.append("merchant/userAgent");//http://www.***.cn/wechat/merchant/userAgent
		qrurl.append("?merchantNo=" + qrCodeEntity.getMerchantId());// 商户号
		
		qrCodeEntity.setCodeNo(codeNo);
		qrCodeEntity.setUrl(qrurl.toString());         //访问路径
		qrCodeEntity.setDeleteStatus(Constant.DELETE_STATUS_NO);	//删除状态  0：未删除1：已删除 
		qrCodeEntity.setCodeStatus(Constant.CODE_STATUS_SD);		//二维码状态  0、系统生成 1、手动添加
		qrCodeDao.insertQrCode(qrCodeEntity);
	}

	@Override
	public List<QRCodeEntity> queryList(Map<String, Object> map) {
		return qrCodeDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return qrCodeDao.queryTotal(map);
	}

	@Override
	public int deleteBatch(String[] ids) {
		return qrCodeDao.deleteBatch(ids);
	}

	@Override
	public boolean addBatchCode(QRCodeEntity qrCode) {
		String merchantNo = qrCode.getMerchantId();
		//根据银行卡id、商户号查询该商户下还未添加二维码的 收款员集合
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("merchantNo", merchantNo);
		map.put("bankId", qrCode.getBankId());
		map.put("deleteStatus", Constant.DELETE_STATUS_NO);
		List<CashiersEntity> cashList = cashiersDao.queryListByParams(map);
		if(cashList.size() > 0){
	        StringBuffer qrurl = new StringBuffer();
			//下载时访问地址
			qrurl.append("merchant/userAgent");//http://www.***.cn/wechat/merchant/userAgent   Constant.DOMAINURL+
			qrurl.append("?merchantNo=" + merchantNo);// 商户号
			
			qrCode.setCodeName("主码");        //收款码名称
			qrCode.setUrl(qrurl.toString());         //访问路径
			qrCode.setDeleteStatus(String.valueOf(Constant.DELETE_STATUS_NO));	//删除状态  0：未删除1：已删除 
			qrCode.setCodeStatus(Constant.CODE_STATUS_SD);		//二维码状态  0、系统生成 1、手动添加
			
			for(CashiersEntity cash : cashList){
				String date = DateUtils.format(new Date(), "yyyyMMdd");
		        String random = RandomStringUtils.randomNumeric(6);
		        String codeNo = date + random;
		        qrCode.setCodeNo(codeNo);       //收款码编号
		        
				qrCode.setUserId(String.valueOf(cash.getId()));
				if(qrCodeDao.insertQrCode(qrCode) < 0){
					return false;
				}
			}
		}
		return true;
	}
}
